package thread;

/**
 * 互斥性
 *
 */
public class SyncDemo4 {
    public static void main(String[] args) {
        Foo f = new Foo();
        Thread t = new Thread(){
            public void run(){
                f.methodA();
            }
        };

        Thread t1 = new Thread(){
            public void run(){
                f.methodB();
            }
        };

        t.start();
        t1.start();
    }
}

class Foo{
    public synchronized void methodA(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+":正在执行A方法。。。。");
            Thread.sleep(2000);
            System.out.println(t.getName()+"A方法执行完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void methodB(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName()+":正在执行B方法。。。。");
            Thread.sleep(2000);
            System.out.println(t.getName()+"B方法执行完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
